%===================================================================================================
%[]FUNCTION NAME: Coe2State.m
%[]AUTHOR: Julio C�sar Benavides
%[]CREATED: 06/24/2005
%[]REVISED: 02/02/2018
%===================================================================================================
%[]FUNCTION DESCRIPTION:
%This function calculates the inertial states associated with a set of classical orbital elements.
%===================================================================================================
%[]INPUT VARIABLES:
%(Coe)|Classical orbital elements matrix.
%---------------------------------------------------------------------------------------------------
%(u)|Gravitational parameter.
%===================================================================================================
%[]OUTPUT VARIABLES:
%(S)|Inertial states matrix.
%===================================================================================================
%[]VARIABLE FORMAT:
%(Coe)|Matrix {6 x n}.
%---------------------------------------------------------------------------------------------------
%(u)|Scalar {1 x 1}.
%---------------------------------------------------------------------------------------------------
%(S)|Matrix {6 x n}.
%===================================================================================================
%[]AUXILIARY FUNCTIONS:
%None.
%===================================================================================================
%[]COMMENTS:
%None.
%===================================================================================================
function S = Coe2State(Coe,u)
    
    n = size(Coe);
    %[]Returns the dimensions of the COE matrix.
    
    S = zeros(n);
    %[km,km/s]Allocates memory for the state vectors corresponding the COE matrix.
    
    for k = 1:n(2)
        
        p = Coe(1,k) * (1 - Coe(2,k)^2);
        %[km]Current semiparameter WRT the central body.
        
        r = p / (1 + Coe(2,k) * cosd(Coe(6,k)));
        %[km]Current range WRT the central body.
        
        Rpqw = r * [cosd(Coe(6,k)); sind(Coe(6,k)); 0];
        %[km]Current position vector WRT the central body in the PQW.
        
        Vpqw = sqrt(u / p) * [-sind(Coe(6,k)); Coe(2,k) + cosd(Coe(6,k)); 0];
        %[km/s]Current velocity vector WRT the central body in the PQW.
        
        Spqw = [Rpqw; Vpqw];
        %[km,km/s]Current state vector WRT the central body in the PQW.
        
        R3Om = [cosd(Coe(4,k)), -sind(Coe(4,k)), 0; sind(Coe(4,k)), cosd(Coe(4,k)), 0; 0, 0, 1];
        %[]Rotation matrix about the third axis by an angle right ascension of the ascending node.
        
        R1In = [1, 0, 0; 0, cosd(Coe(3,k)), -sind(Coe(3,k)); 0, sind(Coe(3,k)), cosd(Coe(3,k))];
        %[]Rotation matrix about the first axis by an angle inclination
        
        R3om = [cosd(Coe(5,k)), -sind(Coe(5,k)), 0; sind(Coe(5,k)), cosd(Coe(5,k)), 0; 0, 0, 1];
        %[]Rotation matrix about the third axis by an angle argument of periapsis.
        
        T = R3Om * R1In * R3om;
        %[]Current matrix that transforms vectors from the PQW to the IJK.
        
        S(:,k) = [T, zeros(3,3); zeros(3,3), T] * Spqw;
        %[km,km/s]Current state vector WRT the central body in the IJK.
        
    end
    
end
%===================================================================================================